package com.giteeoauth.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/7/28 9:27
 **/
@Slf4j
@Component
public class GiteeHttpClient {
    /**
     * 获取Access Token
     * post
     */
    public static String getAccessToken(String url) throws IOException {

        String result = HttpUtil.post(url, new HashMap<>(0));
        log.info("码云响应报文:{}", result);

        String accessToken = JSONUtil.parseObj(result).getStr("access_token");
        return accessToken;
    }

    /**
     * 获取用户信息
     * get
     */
    public static JSONObject getUserInfo(String url) throws IOException {
        String resp = HttpUtil.get(url);
        log.info("码云获取个人信息返回报文{}", resp);

        JSONObject userInfo = JSONUtil.parseObj(resp);
        return userInfo;
    }

}
